﻿using Aurora.AI.SqlSugar.Framework;
using Aurora.AI.SqlSugar.Framework.Repositories;

namespace Aurora.AI.Wiki.Domain.Domain.Wikis.Repositories;

/// wiki repository
public class WikiRepository : Repository<Aggregates.Wiki>, IWikiRepository
{
    /// <inheritdoc />
    public async Task<List<Aggregates.Wiki>> GetListAsync(string? keyword, int page, int pageSize)
    {
        return await GetDb().Queryable<Aggregates.Wiki>()
            .WhereIF(!string.IsNullOrWhiteSpace(keyword), x => x.Name.Contains(keyword!))
            .ToPageListAsync(page, pageSize);
    }

    /// <inheritdoc />
    public Task<int> GetCountAsync(string? keyword)
    {
        return GetDb().Queryable<Aggregates.Wiki>().WhereIF(!string.IsNullOrWhiteSpace(keyword), x => x.Name.Contains(keyword!))
            .CountAsync();
    }

    public async Task<List<Aggregates.WikiDetail>> GetDetailsListAsync(string wikiId, string? keyword, int page, int pageSize)
    {
        return await GetDb().Queryable<Aggregates.WikiDetail>()
            .WhereIF(!string.IsNullOrWhiteSpace(keyword), x => x.FileName.Contains(keyword!))
            .ToPageListAsync(page, pageSize);
    }

    public async Task<long> GetDetailsCountAsync(string wikiId, string? keyword)
    {
        return await GetDb().Queryable<Aggregates.WikiDetail>()
            .WhereIF(!string.IsNullOrWhiteSpace(keyword), x => x.FileName.Contains(keyword!))
            .CountAsync();
    }

    /// <inheritdoc />
    public async Task<Aggregates.WikiDetail> AddDetailsAsync(Aggregates.WikiDetail wikiDetail)
    {
        await GetDb().Insertable(wikiDetail).ExecuteCommandAsync();
        return wikiDetail;
    }

    public async Task RemoveDetailsAsync(string wikiDetailId)
    {
        await GetDb().Deleteable<Aggregates.WikiDetail>().Where(x => x.Id.Equals(wikiDetailId)).ExecuteCommandAsync();
    }

    public async Task<Aggregates.WikiDetail> GetDetailsAsync(string wikiDetailId)
    {
        return await GetDb().Queryable<Aggregates.WikiDetail>().FirstAsync(x => x.Id.Equals(wikiDetailId));
    }

    public async Task RemoveDetailsAsync(List<string> wikiDetailIds)
    {
        await GetDb().Deleteable<Aggregates.WikiDetail>().Where(x => wikiDetailIds.Contains(x.Id)).ExecuteCommandAsync();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="serviceProvider"></param>
    public WikiRepository(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }
}